﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qltscd.Models;
using qltscd.Models.ScreenClient;
using qltscd.Contansts;
using Telerik.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using qltscd.Helper;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace qltscd.Controllers
{
    public class DanhMucNuocSanXuatController : BaseController
    {
        #region Properties Section
        private QLTBEntities db = new QLTBEntities();
        #endregion

        #region Views Section
        public ActionResult Index()
        {
            Session[SessionConstants.ERROR_MESSAGE] = string.Empty;
            DMNSXScreenClient screen = new DMNSXScreenClient();
            if (Session[SessionScreenContansts.DanhMucNuocSanXuat_SCREENCLIENT] != null)
            {
                screen = (DMNSXScreenClient)Session[SessionScreenContansts.DanhMucNuocSanXuat_SCREENCLIENT];
            }
            Search(screen);
            return View(screen);
        }

        [HttpPost]
        public ActionResult Index(DMNSXScreenClient screen)
        {
            Search(screen);
            return View(screen);
        }

        [GridAction]
        public ActionResult _Index()
        {
            DMNSXScreenClient screen = new DMNSXScreenClient();
            if (Session[SessionScreenContansts.DanhMucNuocSanXuat_SCREENCLIENT] != null)
            {
                screen = (DMNSXScreenClient)Session[SessionScreenContansts.DanhMucNuocSanXuat_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DSNuocSanXuat));
        }
        public ActionResult Create()
        {
            Models.BaseClient.DanhMucNuocSanXuat nsx = new Models.BaseClient.DanhMucNuocSanXuat() { IdentityNuocSanXuat = -1 };
            return View(nsx);
        }

        public ActionResult SuaNuocSanXuat()
        {
            string id = Request.QueryString["id"];
            if (id != null)
            {
                int iden = Convert.ToInt32(id);
                DanhMucNuocSanXuat nsx = db.DanhMucNuocSanXuats.Single(i => i.IdentityNuocSanXuat == iden);
                Models.BaseClient.DanhMucNuocSanXuat nsxC = new Models.BaseClient.DanhMucNuocSanXuat();
                ObjectHelper.Copy(nsx, nsxC);
                return View(nsxC);

            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult ShowPdf()
        {
            ReportClass rpt = new ReportClass();
            rpt.FileName = Server.MapPath("~/") + "//Reports//DanhMucNuocSanXuat.rpt";
            rpt.Load();
            //rpt.SetDataSource(db.DanhMucNuocSanXuats);
            Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            return File(stream, "application/pdf");
        }

        public FileResult ShowExcel()
        {
            //string now = DateTime.Now.ToString("dd.MM.yyyy_HH.mm.ss_f.ff.fff.ffff");

            //using (ReportClass rptH = new ReportClass())
            //{
            //    rptH.FileName = Server.MapPath("~/") + "//Reports//rDanhMucNuocSanXuat.rpt";
            //    rptH.Load();
            //    //rptH.SetDataSource(db.DanhMucNuocSanXuats);
            //    string url = Server.MapPath("~/") + "//ExportToExcel" + "//" + now + "_" + qltscd.Helper.SessionHelper.UserId + "_BaoCaoDanhMucNuocSanXuat.xls";
            //    rptH.ExportToDisk(ExportFormatType.Excel, url);
            //    return File(url, "application/ms-excel", "BaoCaoDanhMucNuocSanXuat.xls");
            //}
            ReportClass rpt = new ReportClass();
            rpt.FileName = Server.MapPath("~/") + "//Reports//rDanhMucNuocSanXuat.rpt";
            rpt.Load();
            //rpt.SetDataSource(db.DanhMucNuocSanXuats);
            Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
            return File(stream, "application/ms-excel", "BaoCaoDanhMucNuocSanXuat.xls");
        }

        public ActionResult ThongTinNuocSanXuat(int id)
        {
            DanhMucNuocSanXuat nsx = db.DanhMucNuocSanXuats.Single(i => i.IdentityNuocSanXuat == id);
            return View(nsx);
        }
        #endregion

        #region CRUD Section
        [HttpPost]
        public ActionResult SuaNuocSanXuat(Models.BaseClient.DanhMucNuocSanXuat nsxC)
        {
            if (ModelState.IsValid)
            {
                //code de luu lai
                DanhMucNuocSanXuat nsx = db.DanhMucNuocSanXuats.Single(i => i.IdentityNuocSanXuat == nsxC.IdentityNuocSanXuat);
               
                ObjectHelper.Copy(nsxC, nsx);
                db.ObjectStateManager.ChangeObjectState(nsx, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(nsxC);
        }

        [HttpPost]
        public ActionResult Create(Models.BaseClient.DanhMucNuocSanXuat danhmucNuocSanXuat)
        {
            if (ModelState.IsValid)
            {
                DanhMucNuocSanXuat nsx = new DanhMucNuocSanXuat();
                if (db.DanhMucNuocSanXuats.Count(o => o.MaNuocSanXuat == danhmucNuocSanXuat.MaNuocSanXuat) > 0)
                {
                    ViewData[SessionConstants.ERROR_MESSAGE] = "Mã nước sản xuất " + danhmucNuocSanXuat.MaNuocSanXuat + " đã tồn tại";
                    return View(danhmucNuocSanXuat);
                }
                ObjectHelper.Copy(danhmucNuocSanXuat, nsx);
                db.DanhMucNuocSanXuats.AddObject(nsx);
                db.SaveChanges();
                return RedirectToAction("Create");
            }
            return View(danhmucNuocSanXuat);
        }
        public ActionResult Delete(int id)
        {
            DanhMucNuocSanXuat nsx = db.DanhMucNuocSanXuats.Single(i => i.IdentityNuocSanXuat == id);
            db.DanhMucNuocSanXuats.DeleteObject(nsx);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult XoaNuocSanXuat()
        {
            string id = Request.QueryString["id"];
            try
            {
                if (id != null)
                {
                    int iden = Convert.ToInt32(id);
                    DanhMucNuocSanXuat nsx = db.DanhMucNuocSanXuats.Single(i => i.IdentityNuocSanXuat == iden);
                    db.DanhMucNuocSanXuats.DeleteObject(nsx);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
        #endregion

        #region Helper Section
        private void Search(DMNSXScreenClient screen)
        {
            if (screen == null) { screen = new DMNSXScreenClient(); }
            var list = db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat);
            screen.DSNuocSanXuat.Clear();
            foreach (DanhMucNuocSanXuat hang in list)
            {
                qltscd.Models.BaseClient.DanhMucNuocSanXuat hangClient = new Models.BaseClient.DanhMucNuocSanXuat();
                ObjectHelper.Copy(hang, hangClient);
                screen.DSNuocSanXuat.Add(hangClient);
            }
            Session[SessionScreenContansts.DanhMucNuocSanXuat_SCREENCLIENT] = screen;
        }
        #endregion

        public void clearFolder()
        {
            DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/") + "//Reports");

            foreach (FileInfo fi in dir.GetFiles())
            {
                fi.Delete();
            }

            //foreach (DirectoryInfo di in dir.GetDirectories())
            //{
            //    clearFolder(di.FullName);
            //    di.Delete();
            //}
        }


    }
}
